﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.Dnn;
namespace WinCoreV3Run
{




   public class TestResult
    {
        public   int classIndex;
        public double probility;
        public TestResult(int classIndex, double probility)
        {
            this.classIndex = classIndex;
            this.probility = probility;
        }
        public string toString()
        {
           
            string res = string.Format("识别种类={0},可信度={1}", classIndex, probility);
            return res;
        }
    };

    public class MyDNN
    {
        public Net cnn_net;
        public  int Init_CNN(string tf_pbfile)
        {
            cnn_net = Emgu.CV.Dnn.DnnInvoke.ReadNetFromTensorflow(tf_pbfile);
            if (cnn_net.Empty)
            {

                return -1;
            }
            else
            {
                return 1;
            }

        }
        public TestResult Test_Img(Mat src)
        {

            bool swapRGB = true;
            if (MyParam.swapRGB == 0)
            {
                swapRGB = false;
            }
            else
            {

                swapRGB = true;
            }

            try
            {
             
             
                Mat blob = DnnInvoke.BlobFromImage(src,
              1.0 / 255, new Size(224, 224), new Emgu.CV.Structure.MCvScalar(), swapRGB);
                cnn_net.SetInput(blob, "input_1");
                Mat output = cnn_net.Forward("MobileNetV3_Small/LastStage/Squeeze/Squeeze_1");
                Point classNumber = new Point();
                double classProb = 0;
                double nullProb = 0;
                Point nullNumber = new Point();
                CvInvoke.MinMaxLoc(output, ref nullProb, ref classProb, ref nullNumber, ref classNumber); // 可能性最大的一个
                TestResult res = new TestResult(classNumber.X, classProb);
                return res;


            }
            catch (Exception er)
            {
                TestResult res = new TestResult(0, 1);
                return res;
            }
          

        }
    }
}
